<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'jh:plan:jhPlanArea:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="90%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <div>
      <BasicTable @register="registerTable" @row-click="handleRdrecordRowClick">
        <template #headerCell="{ column }">
          <template v-if="column.dataIndex === 'tzQty'">
            <span class="fontWeight-bold">
              {{ column.customTitle }}
            </span>
          </template>
          <template v-else>
            {{ column.customTitle }}
          </template>
        </template>
        <template #summary>
          <Table.Summary fixed>
            <Table.Summary.Row>
              <Table.Summary.Cell :index="0" :col-span="1" :style="{ textAlign: 'center' }"
                >合计</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="2" :col-span="8"></Table.Summary.Cell>
              <Table.Summary.Cell :index="10" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.fqty }}</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="11" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.xcQty }}</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="12" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.syBhQty }}</Table.Summary.Cell>
              
              <Table.Summary.Cell :index="12" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                  {{ totalIqty.sumpickQty }}</Table.Summary.Cell>
                
              <Table.Summary.Cell :index="13" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.tzQty }}
              </Table.Summary.Cell>
            </Table.Summary.Row>
          </Table.Summary>
        </template>
      </BasicTable>
    </div>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsRdRd32Rdrecord32TzList',
  });
</script>
<script lang="ts" setup>
  import { unref, defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { companyTreeData } from '/@/api/sys/company';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { jhPlanAreaListBhsData, jhPlanAreaConfirm } from '/@/api/wms/jh/plan/jhPlanArea';
  import { Table } from 'ant-design-vue';
  const { t } = useI18n('rd.rd32.rdrecord32');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const emit = defineEmits(['success', 'register']);
  const groupFlag = ref<string>('');

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: t('明细调整'),
  };
  const tableColumns: BasicColumn[] = [
     {
      title: t('销售订单号'),
      dataIndex: 'forderno',
      key: 'a.forderno',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('发货单号'),
      dataIndex: 'parent.fbillno',
      key: 'parent.fbillno',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('联系人'),
      dataIndex: 'fperson',
      key: 'a.fperson',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('地址'),
      dataIndex: 'faddress',
      key: 'a.faddress',
      sorter: true,
      width: 160,
      align: 'left',
    },
     {
      title: t('电话'),
      dataIndex: 'fphone',
      key: 'a.fphone',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('商品编码'),
      dataIndex: 'fitemno',
      key: 'a.fitemno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('商品名称'),
      dataIndex: 'fitemname',
      key: 'a.fitemname',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('形象刊'),
      dataIndex: 'freeVO.cfree1',
      width: 130,
      align: 'left',
    },
    {
      title: t('应拣数量'),
      dataIndex: 'fqty',
      key: 'a.fqty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('现存量'),
      dataIndex: 'xcQty',
      key: 'a.xcQty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('差缺数'),
      dataIndex: 'xclDiffQty',
      key: 'a.xclDiffQty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('可拣货数'),
      dataIndex: 'pickQty',
      key: 'a.pickQty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('调整数'),
      dataIndex: 'tzQty',
      key: 'a.tzQty',
      sorter: true,
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 8,
      },
      editRule: false,
    },
    {
      title: t('仓库'),
      dataIndex: 'fstockname',
      key: 'a.pickQty',
      // sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  // const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    groupFlag.value = data.groupFlag;
    setProps({
      beforeFetch: (params) => {
        params.groupFlag = data.groupFlag;
        return params;
      },
    });

    reload();
    setDrawerProps({ loading: false });
    console.log('data', data);
  });
  const [registerTable, { reload, getForm, setProps, getDataSource }] = useTable({
    api: jhPlanAreaListBhsData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    maxHeight: 800,
    // actionColumn: actionColumn,
    // formConfig: searchForm,
    pagination: false,
    showTableSetting: false,
    useSearchForm: false,
    canResize: true,
    immediate: false,
    resizeHeightOffset: 70
  });

  function handleRdrecordRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  async function handleSubmit() {
    const data = await getDataSource();
    data.forEach((item) => {
      // console.log('item', item.editValueRefs.tzQty);
      item.tzQty = item.editValueRefs.tzQty;
    });
    // data 转换成 json
    const res = await jhPlanAreaConfirm({
      groupFlag: groupFlag.value,
      data: JSON.stringify(data),
    });
    // showMessage(res.message);
    closeDrawer();
    emit('success', groupFlag.value);
  }

  const totalIqty = computed(() => {
    // 计算总报工数
    const tableData = getDataSource();
    console.log('tableData', tableData);
    // const tableData = getDataSource.value;
    let sumFqty = tableData.reduce((total, item) => total + (item.fqty || 0), 0);
    let sumXcQty = tableData.reduce((total, item) => total + (item.xcQty || 0), 0);
    let sumSyBhQty = tableData.reduce((total, item) => total + (item.xclDiffQty || 0), 0);
    let sumpickQty = tableData.reduce((total, item) => total + (item.pickQty || 0), 0);
    
    let sumTzQty = tableData.reduce((total, item) => total + (Number(item.editValueRefs.tzQty) || 0), 0);
    console.log('sumTzQty', sumTzQty);
    return {
      fqty: sumFqty,
      xcQty: sumXcQty,
      syBhQty: sumSyBhQty,
      sumpickQty: sumpickQty,
      tzQty: sumTzQty,
    };
  });
</script>
<style scoped>
  .ant-input{
    border: 1px solid red !important;
  }
  .fontWeight-bold{
    font-weight: bold;
  }
</style>
